package com.sky.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class ComputeTimeAspect {
    @Pointcut("execution(* com.sky.service.*.*(..))")
    private void pt(){}

    @Around("pt() && @annotation(com.sky.annotation.ComputeTime)")
    public Object computeTime(ProceedingJoinPoint joinPoint) throws Throwable {
        String methodName = joinPoint.getSignature().getName();
        Long begin = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        Long end = System.currentTimeMillis();
        Long cost = end - begin;
        log.info("方法 {} 执行耗时: {} ", methodName, cost);
        return result;
    }
}
